<?php

    /**
     * Class Navigate_model
     */
    class Navigate_model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
        }

        /**
         * 刚进入页面显示上面的一级导航,由admin/index调用
         * @param int $pid
         * @return mixed
         */
        function getList( $pid = 0 )
        {

            $this->db->where( [ 'pid' => $pid,'status'=>1 ] );
            $this->db->order_by('listorder asc,id asc');
            return $this->db->get("auth_rule")->result_array();



        }

        /**
         * 显示二级菜单
         * @param $pid
         * @return mixed
         */
        function getChildList( $pid )
        {
            $this->db->where( [ 'pid' => $pid,'status'=>1 ] );
            $this->db->order_by('listorder asc,id asc');
            return $this->db->get("auth_rule")->result_array();
            return $this->db->get_where( "auth_rule", [ 'pid' => $pid ] )->result_array();
        }

        /**
         * 后台菜单管理显示菜单列表
         * @return mixed
         */
        function getAllList( $pid = 0 )
        {
            return $this->db->get( "auth_rule" )->result_array();
        }

        /**
         * 显示递归
         * @param $selected_id
         */
        function getTree( $select_id = 0 )
        {

            $result = $this->db->get( "auth_rule" )->result_array();
            $tree = $this->_tree( $result, 0 );
            $tree_str = "";
            foreach ( $tree as $k => $v ) {
                if ( $v[ 'id' ] == $select_id ) {
                    $tree_str .= "<option selected='selected' id='" . $v[ 'id' ] . "'>";
                } else {
                    $tree_str .= "<option id='" . $v[ 'id' ] . "'>";
                }

                if ( $v[ 'pid' ] == 0 ) {
                    $tree_str .= $v[ 'text' ];
                } elseif ( $v[ 'islast' ] == 0 ) {
                    $tree_str .= str_repeat( '┃ ', $v[ 'lev' ] ) . "┣" . $v[ 'text' ];
                } elseif ( $v[ 'islast' ] == 1 ) {
                    $tree_str .= str_repeat( '┃ ', $v[ 'lev' ] ) . "┗" . $v[ 'text' ];
                }
                $tree_str .= "</option>";

            }

            return $tree_str;
        }

        /**
         * 得到树
         * @param        $arr
         * @param int    $pid
         * @param int    $lev
         * @param string $islast
         * @return array
         */
        function _tree( $arr, $pid = 0, $lev = 0, $islast = '' )
        {
            static $tree = [ ];
            foreach ( $arr as $v ) {
                if ( $v[ 'pid' ] == $pid ) {
                    $v[ 'lev' ] = $lev;
                    if ( $this->_isLastChildNode( $arr, $v[ 'id' ] ) ) {
                        $v[ 'islast' ] = 1;
                    } else {
                        $v[ 'islast' ] = 0;
                    }
                    $tree[ ] = $v;
                    $this->_tree( $arr, $v[ 'id' ], $lev + 1 );
                }
            }

            return $tree;

        }

        /**判断是否是平级的最后一个元素
         * @param $arr
         * @param $id
         * @return bool
         */
        function _isLastChildNode( $arr, $id )
        {
            $pid = '';
            foreach ( $arr as $k => $v ) {
                if ( $v[ 'id' ] == $id ) {
                    $pid = $v[ 'pid' ];
                }
            }
            $tmp = [ ];
            foreach ( $arr as $k => $v ) {
                if ( $v[ 'pid' ] == $pid ) {
                    $tmp[ ] = $v;
                }
            }
            if ( end( $tmp )[ 'id' ] == $id ) {
                return true;
            } else {
                return false;
            }
        }

        public function getNodeById( $id )
        {
            return $this->db->get_where( "auth_rule", [ 'id' => $id ] )->result_array();
        }
        public function delete($ids){
            $this->db->delete('auth_rule')->where_in('id',implode($ids));
            echo $this->db->last_query();exit;
        }

    }

?>